﻿using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using THANHTUNGO.Portal.Components.Security;
using System;

namespace ITG.Modules.HR.Components.SmartBoss
{
    public class Department
    {
        public static DataTable GetAccessingDepartments(int userID)
        {
            string[] departments = Accessing.SelectDepartmentBy_UserID(Convert.ToInt32(PortalSecurity.CurrentUser.Identity.ID));

            string query = "SELECT cDepartmentID AS ID, cDepartment AS Name FROM tblDepartment";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase("SmartBoss");
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(query);
            DataSet ds = db.ExecuteDataSet(dbCommand);

            DataTable dt = new DataTable();
            DataColumn dcID = new DataColumn("ID", typeof(int));
            DataColumn dcName = new DataColumn("Name", typeof(string));
            dt.Columns.Add(dcID);
            dt.Columns.Add(dcName);

            foreach (string d in departments)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    if (row["ID"].ToString().Equals(d))
                    {
                        DataRow dr = dt.NewRow();
                        dr["ID"] = row["ID"].ToString();
                        dr["Name"] = row["Name"].ToString();
                        dt.Rows.Add(dr);
                        break;
                    }
                }
            }
            return dt;
        }
    }
}